//
//  DiscussionViewController.m
//  ForengnMenu
//
//  Created by @-@ on 16/1/14.
//  Copyright © 2016年 余旺. All rights reserved.
//

#import "DiscussionViewController.h"
#import "scorolViewCell.h"
#import "DiscussionCell.h"
#import "SearchViewController.h"
#import "contentPushContller.h"

@interface DiscussionViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,UIPageViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *mytabelView;
@property UIScrollView * ScollView;

@property UIPageControl * PageControl;
@property NSString* indent;
@property NSDictionary * dicone;

@property NSDictionary * dictwo;
@property NSArray * arr;
@property NSArray * arr1;
@property float maxX;

@property float minN;
@end

@implementation DiscussionViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"headImage.jpg"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 30, 30);
    button.layer.cornerRadius = button.frame.size.height/2.0f;
    button.layer.masksToBounds = YES;
    
    [button addTarget:self action:@selector(headAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.mytabelView.backgroundColor = [UIColor colorWithRed:246/255.0f green:247/255.0f blue:239/255.0f alpha:1.0f];
    
    //返回键代码
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"取消";

    self.navigationItem.backBarButtonItem = backItem;
    
    UIButton * centerBnt = [UIButton buttonWithType:UIButtonTypeCustom];
    [centerBnt setImage:[UIImage imageNamed:@"Icon_Search_Box"] forState:UIControlStateNormal];
    
    centerBnt.frame = CGRectMake(0, 0, 215, 30);
    
    [centerBnt addTarget:self action:@selector(serarchBoxAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 205, 30)];
    
    [view addSubview:centerBnt];
    
    self.navigationItem.titleView = view;
    
    //发布按钮
    UIButton * rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightbutton setImage:[UIImage imageNamed:@"fabu@_"] forState:UIControlStateNormal];
    rightbutton.frame = CGRectMake(0, 0, 20, 20);
    
    [rightbutton addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
    
    NSDictionary * dic1 = @{@"name":@"日料大作战",
                            @"zan":@"赞:156",
                            @"context":@"       日本料理是用眼睛品尝的料理，更准确应该是用五感来品尝的料理，强调色、味、形、器。日本料理烹调的特色着重自然的原味，不容置疑的，原味是日本料理首要的特点。在这吃不下重口味料理的炎炎夏日，让我们一起感受原汁原味的日式料理，在家来场清爽精致日料大作战吧！。",
                            @"imges":@"img@.jpg",
                            @"position":@"两岸咖啡",
                            @"time":@"2个小时前",
                            @"answer":@"123回复",
                            @"attention":@"关注",
                            @"autograph":@"爱生活，爱美丽",
                            @"ntroduce":@"新鲜美味三文鱼刺身",
                            @"materia":@"       刺身是来自日本的一种传统食品，最出名的日本料理之一，它将鱼（多数是海鱼）、乌贼、虾、章鱼、海胆、蟹、贝类等肉类利用殊刀工切成片、条、块等形状，蘸着山葵泥、酱油等佐料，直接生食。日语汉字将“刺身”写作“刺身”罗马音为sashimi，中国一般将“刺身”叫作“生鱼片”，因为刺身原料主要是海鱼，而刺身实际上包了一切可以生吃的肉类，甚至有鸡大腿刺身、马肉刺身、牛肉刺身",
                            @"ntroduceone":@"食材明细",
                            @"shicai":@"       食材指制作食物时所需要使用的原料，如黄瓜，白菜，牛肉，桂圆·.....食材可根据不同的特性分成不同的类别。如：素类食材，肉类食材",
                            @"zuofa":@"做法",
                            @"dishImage":@"cai@.png",
                            @"name1":@"爱琴海",
                            @"imags1":@"dis9.jpg",
                            @"context1":@"这个真的很好吃",
                            @"htime1":@"1个小时前",
                            @"name2":@"不以一生",
                            @"imags2":@"dis8.jpg",
                            @"context2":@"大神，请收我为徒吧",
                            @"htime2":@"3小时前",
                            @"greens1":@"two",
                            @"greens2":@"shrr",
                            @"greens3":@"for",
                            @"greens4":@"fowu",
                            @"greens5":@"7@",
                            @"greens6":@"haha1",
                            @"greens7":@"haha2",
                            @"greens1text":@"1.冰鲜的三文鱼半扇，拔去大刺",
                            @"greens2text":@"2.将三文鱼切成大块诺干，取一块生食，其他用保鲜膜裹好放入",
                            @"greens3text":@"3.备用的三文鱼去皮（切成大块后好去皮，整扇的话比较靠功夫",
                            @"greens4text":@"4.将三文鱼切成0.3-0.5cm左右薄厚的片",
                            @"greens5text":@"5.取一深盘放入冰块，上铺保鲜膜",
                            @"greens6text":@"6.洗净的紫苏叶铺在冰上，中间用长条鱼片卷成花型",
                            @"greens7text":@"7.紫苏叶上码入三文鱼片，取少许紫苏叶丝装饰在中间，盖上保鲜膜放入冰箱冷藏十分钟",
                            @"celltwo":@"评价(179)",
                            @"dis":@{@"dis1":@"dis1.jpg",
                                     @"dis2":@"dis2.jpg",
                                     @"dis3":@"dis3.jpg",
                                     @"dis4":@"dis4.jpg"
                                     
                                     }
                            };
    
    NSDictionary * dic2 =@{@"name":@"如何做出好吃的",
                           @"zan":@"赞:346",
                           @"context":@"       法国地处南欧的亚平宁半岛，优越的地理条件使意大利的受精和食品加工业都很发达，其以面条，奶酪，色拉米肉肠著称于世。 公元前2世纪后期，因为刺身原料主要是海鱼，而刺身实际上包了一切可以生吃的肉类，甚至有鸡大腿刺身、马肉刺身、古罗马宫廷的膳房已形成庞大的队伍，并有很细的分工。",
                           @"imges":@"imag@!.jpg",
                           @"position":@"笑吧额娘",
                           @"time":@"4个小时前",
                           @"answer":@"66回复",
                           @"attention":@"已关注",
                           @"autograph":@"家常达人，小组生活盟主",
                           @"ntroduce":@" 法国西餐咖喱",
                           @"materia":@"       法国菜在材料的选用较偏好牛肉、羊肉、家禽、海鲜、蔬菜、口螺、松露、鹅肝及鱼籽酱；而在配料方面采用大量的酒、法国菜在材料的选用上较偏好牛肉、蜗牛肉、羊肉、家禽、海鲜、蔬菜、田螺、松露、鹅肝及鱼子酱;而在配料方面采用大量的酒、牛油、鲜奶油及各式香料;在烹调时，火侯占了非常重要的一环，如牛、羊肉通常烹调至六、七分熟即可;",
                           @"dishImage":@"cai@1",
                           @"ntroduceone":@"食材明细",
                           @"shicai":@"       七子白——宫廷传统中药面膜早在中国古代，就有利用白术、白芷、白芨、白蔹、白茯苓、白芍、珍珠粉这七种食材作美白面膜的传统，它可以淡化斑点、美白肌肤、消除痘印。",
                           @"zuofa":@"做法",
                           @"name1":@"里斯",
                           @"imags1":@"dis3.jpg",
                           @"context1":@"给我来一份好吗",
                           @"htime1":@"2小时前",
                           @"name2":@"冰块红酒",
                           @"imags2":@"dis7.jpg",
                           @"context2":@"原汁原味吗",
                           @"htime2":@"5小时前",
                           @"greens1":@"11",
                           @"greens2":@"22",
                           @"greens3":@"33",
                           @"greens4":@"44",
                           @"greens5":@"55",
                           @"greens6":@"66",
                           @"greens7":@"77",
                           @"greens1text":@"1.粉丝炸酥、铲碎，鸡蛋炒熟、韭菜切末、虾皮洗净沥干水分。加少许盐拌匀",
                           @"greens2text":@"2.取一张煎饼，在一角摊上馅料卷起、包好，收口处抹上面糊",
                           @"greens3text":@"3.电饼铛烧热，放包好的煎饼烙至两面金黄",
                           @"greens4text":@"4.用少许纯净水融化拌匀，洗干净苦菊，淋干水分，放入调好的汁搅拌均匀",
                           @"greens5text":@"5.加入醋精和盐",
                           @"greens6text":@"6.再放入白糖和牛肉粉",
                           @"greens7text":@"7.煎饼裁成长条形，卷成卷，放入苦菊装盘，淋上点剩下的调味汁即可 ",
                           @"celltwo":@"评价(67)",
                           @"dis":@{@"dis1":@"dis5.jpg",
                                    @"dis2":@"dis6.jpg",
                                    @"dis3":@"dis7.jpg",
                                    @"dis4":@"dis8.jpg"
                                    
                                    }
                           };
    self.arr = @[dic1,dic2];
}

//搜索
-(void)serarchBoxAction{
    
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStyleDone target:nil action:nil];
    
    UIStoryboard * stroyboard = [UIStoryboard storyboardWithName:@"LearnCook" bundle:[NSBundle mainBundle]];
    
    SearchViewController * viewController = [stroyboard instantiateViewControllerWithIdentifier:@"SearchViewController"];
    
    [self.navigationController pushViewController:viewController animated:YES];

    
}
//发布
-(void)rightAction{
    
    [self performSegueWithIdentifier:@"puth" sender:nil];
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section ==0) {
        return 1;
    }else{
        return 2;
    
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==0) {
      self.indent=@"scorolViewCell";
        
        scorolViewCell * cell=[tableView dequeueReusableCellWithIdentifier:self.indent];

        cell.myScrollView.delegate = self;
        
        
        self.PageControl=cell.myPageControl;
        self.ScollView=cell.myScrollView;
        
        
        float offfsex=cell.myScrollView.contentOffset.x/320.f;
        cell.myPageControl.currentPage= offfsex;
        
        
        [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(runTamAction) userInfo:nil repeats:YES];
        
        
        return cell;
    }else {

        self.indent=@"DisCussionCell";
        
        DiscussionCell * cell1 = [tableView dequeueReusableCellWithIdentifier:self.indent];
        
        NSDictionary * qwe = self.arr[indexPath.row];
        
        cell1.groupName1.text = qwe[@"name"];

        UIImageView * imagview=(UIImageView *)[UIImage imageNamed:qwe[@"dishImage"]];
        
        cell1.dishImage.image =(UIImage *)imagview;
        
        
        cell1.zanLabel.text = qwe[@"zan"] ;
        

        NSString * stringcontent = qwe[@"context"];
        
        cell1.contentLabel.textAlignment = NSTextAlignmentLeft;
        
        cell1.contentLabel.numberOfLines = MAXFLOAT;

        
        cell1.contentLabel.text = stringcontent ;
        
        NSString * imagewithImag=qwe[@"imges"];
        
        
        UIImageView * imagviewiamg=(UIImageView *)[UIImage imageNamed:imagewithImag];
        
        
        cell1.selfImageLabel.layer.cornerRadius = cell1.selfImageLabel.layer.frame.size.height/2.0f;
        cell1.selfImageLabel.layer.masksToBounds = YES;
        
        
        cell1.selfImageLabel.image =(UIImage *)imagviewiamg;

        NSString * stringwithtime = qwe[@"time"];
        
        cell1.timelabel.text = stringwithtime ;
        
        NSString * stringwireping = qwe[@"answer"];
        
        cell1.replyLabel.text = stringwireping ;
        

        
        NSString *stringWithName  = qwe[@"position"];
        
        cell1.nameLabel.text = stringWithName;
        
        return cell1;

    }

}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (section==1) {
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
        view.backgroundColor = [UIColor whiteColor];
        
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 12, 20, 20)];
        imageView.image = [UIImage imageNamed:@"biaoqian"];
        [view addSubview:imageView];
        
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(40, 7, 60, 30)];
        label.text = @"话题小组";
        label.font = [UIFont systemFontOfSize:14];
        [view addSubview:label];
        
        return view;
    }else{
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 14;
    }else{
        return 40;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section ==0) {
        return 120;
    }else{
    
        return 110;
    }
}

//通知
- (void)headAction{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushSlideslip" object:nil];
    
}
-(void)runTamAction {
    long int pag = self.PageControl.currentPage;
    pag++;
    if (pag >= 3) {
        pag = 0;
    }
    self.PageControl.currentPage = pag;
    [self.ScollView setContentOffset:CGPointMake(320*pag, 0) animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    int offsex=scrollView.contentOffset.x/320.0f;
    //设置当前page的高亮位置
    self.PageControl.currentPage=offsex;
    
    if (offsex >= 3){
        self.PageControl.currentPage = 0;
        
        self.ScollView.contentOffset = CGPointMake(0, 0);
    }
    
}
//跳到下一个界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.section==1) {

            [self performSegueWithIdentifier:@"contentpush" sender:self.arr[indexPath.row]];
    }
    
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([segue.identifier isEqualToString:@"contentpush"]) {
        //拿到下级界面
        contentPushContller * xiajijiemian = segue.destinationViewController;
        
        xiajijiemian.contextarr = (NSArray *)sender;
    }
}

@end
